package com.xk857.school.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xk857.config.AuthUtil;
import com.xk857.entities.Comment;
import com.xk857.entities.SysUser;
import com.xk857.entities.TourismComment;
import com.xk857.feign.IFeignSystemController;
import com.xk857.school.mapper.TourismCommentMapper;
import com.xk857.school.req.TourismCommentAdd;
import com.xk857.school.service.ITourismCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xk857.util.base.Result;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 星空小屋-组团旅行评论表 服务实现类
 * </p>
 *
 * @author CV大魔王
 * @since 2021-05-02
 */
@Primary
@Service
public class TourismCommentServiceImpl extends ServiceImpl<TourismCommentMapper, TourismComment> implements ITourismCommentService {

    @Autowired
    private IFeignSystemController feignSystemController;

    @Override
    public List<TourismComment> findByTourismId(String tourismId) {
        if (StringUtils.isBlank(tourismId)) {
            return null;
        }
        return baseMapper.findByTourismId(tourismId);
    }

    @Override
    public Result deleteById(String id) {
        // 要删除的所有评论id
        ArrayList<String> ids = new ArrayList<>();
        //先把要删除的一级评论id放入到集合中
        ids.add(id);
        //递归的子评论 id 加入到集合中
        this.getIds(ids, id);
        //批量删除集合中的id
        baseMapper.deleteBatchIds(ids);
        return Result.ok();
    }

    @Override
    public Result addByUser(TourismCommentAdd comment) {
        if (StringUtils.isEmpty(comment.getContent())) {
            return Result.error("内容不可为空");
        }

        SysUser userInfo = AuthUtil.getUserInfo();
        SysUser user = feignSystemController.findUserByUserId(userInfo.getId());
        TourismComment tourismComment = new TourismComment();
        tourismComment.setParentId(comment.getParentId());
        tourismComment.setUserId(user.getId());
        tourismComment.setNickName(user.getNickName());
        tourismComment.setUserImage(user.getImageUrl());
        tourismComment.setTourismId(comment.getTourismId());
        tourismComment.setContent(comment.getContent());
        tourismComment.setCreateDate(new Date());
        baseMapper.insert(tourismComment);
        return Result.ok("评论成功");
    }

    @Override
    public Result add(TourismComment comment) {
        baseMapper.insert(comment);
        return Result.ok("评论成功");
    }


    /**
     * 递归方法
     * @param ids 要删除的所有评论id
     * @param parentId 父评论id
     */
    private void getIds(List<String> ids, String parentId) {
        //查询子评论的对象
        QueryWrapper<TourismComment> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", parentId);
        List<TourismComment> commentList = baseMapper.selectList(wrapper);
        if (CollectionUtils.isNotEmpty(commentList)) {
            for (TourismComment comment : commentList) {
                String id = comment.getId();
                ids.add(id);
                // 递归继续查询子评论id
                this.getIds(ids, id);
            }
        }
    }
}
